<?php

namespace app\admin\model;

use phpspider\core\requests;
use phpspider\core\selector;
use think\Model;


class SpiderYouku extends Model
{

    

    

    // 表名
    protected $name = 'spider_youku';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    //初次抓包
    public static function spider($id){
        $model = SpiderYouku::where("id",$id)->find();
        if(empty($model)) return false;
        $url = $model["url"];
        $parent_id = $model["id"];
        if(empty($url)) return false;
        $cookie = $model['cookie'];
        if(empty($cookie)) return false;
        if(strpos($url,"?")!==false){
            $url.="&userId=".time();
        }else{
            $url.="?userId=".time();
        }
        try{
            requests::set_cookie("cna",$cookie);
            $content = requests::get($url);
            $hot = selector::select($content, "//span[@class='video-heat-text']");//热度
            $update_date = selector::select($content, "//div[@class='title-info']//span");//更新日期
            $arr = selector::select($content, "//a[@class='box-item']");//集数
            $jishu = 0;  //最新集数
            $brr_ar = [];
            foreach ($arr as $arr_val){
                $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
                $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
                if($a<1 && $b != "预告"){
                    $a = $jishu + 1;
                    $jishu = $a;
                }else{
                    if($a>0 && $jishu<$a && $b != "预告"){
                        $jishu = $a;
                    }
                }
                $brr_ar[$a]=$b;
            }

            $ar = selector::select($content, "//div[@class='anthology-content']");//集数
            //评论数接口有加密验证，无法抓包
            $ar_content = $ar[0]??"";
            //$br = selector::select($ar, "#<a href=\"(.+?)\" data-spm=\"(.+?)\" .+?</a>#","regex");//集数
            $br = selector::select($ar_content, "#<a\s+href=\"(.+?)\"\s+data-spm=\"(.+?)\"\s+.+?</a>#","regex");//集数
            if($br && is_array($br) && isset($br[0]) && isset($br[1]) && is_array($br[0]) && is_array($br[1])){
                foreach ($br[0] as $br0_k=>$br0_val){
                    if(isset($br[1][$br0_k])){
                        $url = $br0_val;
                        $br1_val = $br[1][$br0_k];
                        $br1_val_ar = explode("_",$br1_val);
                        $collection_number = (int)($br1_val_ar[2]??0);
                        $model = SpiderYoukuCollection::where("parent_id",$parent_id)->where("collection_number",$collection_number)->find();

                        $collection_desc = $brr_ar[$collection_number]??"";
                        $data = [
                            "parent_id"=>$parent_id,
                            "url"=>$url,
                            "collection_number"=>$collection_number,
                            "collection_desc"=>$collection_desc
                        ];
                        if(empty($model)){
                            $model = new SpiderYoukuCollection();
                            $model->save($data);
                        }
                    }
                }
            }

            $update_data = [
                "hot"=>(string)$hot,
                "hot_number"=>$hot,
                "update_date"=>$update_date,
                "comment_number"=>0,
                "barrage_number"=>0,
                "current_collection"=>$jishu
            ];
            SpiderYouku::update($update_data,['id'=>$parent_id]);
        }catch (\Exception $e){
            throw $e;
        }
    }
    

    







}
